package com.kaiwukj.android.ufamily.mvp.ui.page.service.order;

import androidx.annotation.NonNull;

import com.kaiwukj.android.mcas.mvp.BasePresenter;
import com.kaiwukj.android.mcas.utils.RxLifecycleUtils;
import com.kaiwukj.android.ufamily.mvp.http.callback.ApiCallBack;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.ListResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.OrderCreateParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.OrderPayParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.MyAddressResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.OrderCreateResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.ui.page.order.OrderContract;

import java.util.Map;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

public class OrderCreatePresenter extends BasePresenter<OrderContract.Model, OrderContract.OrderCreateView> {

    @Inject
    RxErrorHandler mErrorHandler;

    @Inject
    public OrderCreatePresenter(OrderContract.Model model, OrderContract.OrderCreateView view) {
        super(model, view);
    }

    public void createOrder(OrderCreateParams params) {
        mModel.createOrder(params)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<OrderCreateResult>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull OrderCreateResult result) {
                        mRootView.onOrderCreateResult(result);
                    }
                });
    }

    public void payOrder(OrderPayParams payParams) {
        mModel.payOrder(payParams)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Map<String, Object>>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull Map<String, Object> result) {
                        mRootView.onOrderPayResult(result);
                    }
                });
    }

    public void getDefAddr() {
        mModel.fetchMyAddress(1, 1)
                .doOnSubscribe(act -> mRootView.showLoading())
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<ListResp<MyAddressResult>>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull ListResp<MyAddressResult> result) {
                        if (result.getTotalCount() > 0) {
                            MyAddressResult myAddressResult = result.getList().get(0);
                            if (myAddressResult != null && myAddressResult.isDefault()) {
                                mRootView.onFetchDefAddress(myAddressResult);
                            }
                        }
                    }
                });
    }

}
